package com.qinggeng.project.system.service.impl;

import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.project.system.domain.OrdersArerRecord;
import com.qinggeng.project.system.domain.OrdersPoint;
import com.qinggeng.project.system.domain.OrdersPointRecord;
import com.qinggeng.project.system.mapper.OrdersPointRecordMapper;
import com.qinggeng.project.system.service.OrdersPointRecordService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrdersPointRecordServiceImpl implements OrdersPointRecordService {
    @Resource
    private OrdersPointRecordMapper ordersPointRecordMapper;
    @Override
    public boolean insertOrdersPointRecord(OrdersPointRecord ordersPointRecord) {

        String areaName=ordersPointRecord.getAreaName();
        if (StringUtils.isEmpty(areaName)){
            throw  new CustomException("areaName 名称不能为空");
        }

        String  pointName=ordersPointRecord.getPointName();
        if (StringUtils.isEmpty(pointName)){
            throw  new CustomException("pointName 名称不能为空");
        }

        Long  pointId=ordersPointRecord.getPointId();
        if (null==pointId){
            throw  new CustomException("pointId 名称不能为空");
        }
        String recordDate=ordersPointRecord.getRecordDate();

        if (null == recordDate){
            throw  new CustomException("recordDate is  null");
        }
        Example example =new Example(OrdersPointRecord.class);
        example.createCriteria()
                .andEqualTo("pointId",pointId)
                .andEqualTo("recordDate",recordDate);
        OrdersPointRecord o=ordersPointRecordMapper.selectOneByExample(example);
        if (o!=null){
            return  true;
        }
        ordersPointRecord.setCreateTime(DateUtils.getNowDate());
        ordersPointRecord.setRecordTime(DateUtils.getNowDate());
        return ordersPointRecordMapper.insertSelective(ordersPointRecord)>0;
    }

    @Override
    public void insertOrdersPointRecord(OrdersPoint ordersPoint, String recordDate){

        OrdersPointRecord ordersPointRecord=new OrdersPointRecord();
        ordersPointRecord.setAreaName(ordersPoint.getSortArea());
        ordersPointRecord.setPointId(ordersPoint.getPointId());
        ordersPointRecord.setPointName(ordersPoint.getPointName());
        ordersPointRecord.setRecordDate(recordDate);
        if(!this.insertOrdersPointRecord(ordersPointRecord)){
            log.error("新增配送点统计失败");
            throw new CustomException("新增配送点统计失败");
        }
    }

    @Override
    public List<String> listOrdersPointRecordPointId(String recordDate) {
        if (null == recordDate){
            throw  new CustomException("recordDate is  null");
        }
        Example example =new Example(OrdersPointRecord.class);
        example.createCriteria()
                .andEqualTo("recordDate",recordDate);
        List<OrdersPointRecord> listOrdersPointRecord=ordersPointRecordMapper.selectByExample(example);
        List<String> pointIds=new ArrayList<>();
        for (int i = 0; i < listOrdersPointRecord.size(); i++) {
            OrdersPointRecord ordersPointRecord=listOrdersPointRecord.get(i);
            pointIds.add(String.valueOf(ordersPointRecord.getPointId()));
        }
        return pointIds;
    }

}
